import {binder, BondViewGroup, view} from "adajs";
import LoginService from "./state.js";
import Toast from "./../toast";
import User from "../../lib/user";

@view({
    className: "login",
    template: "./template.html",
    style: "./style.scss",
    dataset: {
        service: LoginService
    }
})
class Login extends BondViewGroup {
    oncreated() {
        if (User.isAdmin()) {
            this.context.request.get("/api/admin/user/isLogin").then(info => {
                if (info.code === "101001") {
                    this.dispatchEvent("gotoPage", User.getUserMap().indexURL);
                }
            });
        } else {
            this.context.request.get("/api/lgb/app/loginCheck").then(info => {
                if (info.code === "101001") {
                    this.dispatchEvent("gotoPage", User.getUserMap().indexURL);
                }
            });
        }
    }

    @binder("checked")
    checked() {
        this.commit("checked");
    }

    @binder("login")
    login() {
        let username = this.finder("username").getElement().value;
        let password = this.finder("password").getElement().value;
        let code = this.finder("code").getElement().value;
        if (username && password && code) {
            User.login(this, {username, password, code});
        } else {
            this.addChild(Toast, {
                parameter: {content: "用户名密码或验证码不能为空"}
            });
        }
    }

    @binder("refreshCode")
    refreshCode() {
        this.commit("refreshCode");
    }
}

export default Login;